package com.NewsDaoImpl;

import com.NewsDao.NewsDao;
import com.bxl.BaseDao;
import com.entity.News;
import com.entity.NewsDetail;
import com.entity.User;
import com.mysql.jdbc.PreparedStatement;
import com.mysql.jdbc.Statement;

import java.sql.Connection;
import java.sql.ResultSet;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class NewsDaoImpl extends BaseDao implements NewsDao {
    @Override
    public List<News> selectAll() {
        // jdbc----mybatis/mybatis-plus
        List<News> newsList = new ArrayList<News>();
        ResultSet resultSet=null;
        try {
            //参数
            //第一个参数：
            String sql="SELECT * FROM `news_detail`";
            //第二个参数：
            Object[] obj={};
             resultSet=this.selectObj(sql,obj);
            while (resultSet.next()){
              News  news = new News();
              int id=  resultSet.getInt("id");
              int categoryId=  resultSet.getInt("categoryId");
              String title=  resultSet.getString("title");
              String summary=  resultSet.getString("summary");
              String content=  resultSet.getString("content");
              Date createdate= resultSet.getDate("createdate");
              String picPath= resultSet.getString("picPath");
              //利用实体类封装数据
                news.setId(id);
                news.setCategoryId(categoryId);
                news.setTitle(title);
                news.setSummary(summary);
                news.setContent(content);
                news.setCreatedate(createdate);
                news.setPicPath(picPath);
              //将javabean对象存储到集合中---临时存储
                newsList.add(news);
            }
        }catch (Exception e){
           e.printStackTrace();
        }finally {
            PreparedStatement pre=null;
            Statement statement =null;
            Connection connectio=null;
            this.closeConnection(pre,statement,resultSet,connectio);
        }
        return newsList;
    }

    @Override
    public News selectById(int id)  {
        News news = new News();
        try {
            String sql="SELECT * FROM `news_detail` WHERE id=?";
            Object[] obj ={id};
            ResultSet resultSet = this.selectObj(sql,obj);
            while (resultSet.next()) {
                int ids = resultSet.getInt("id");
                int categoryId = resultSet.getInt("categoryId");
                String title = resultSet.getString("title");
                String summary = resultSet.getString("summary");
                String content = resultSet.getString("content");
                Date createdate = resultSet.getDate("createdate");
                //利用实体类封装数据
                news.setId(ids);
                news.setCategoryId(categoryId);
                news.setTitle(title);
                news.setSummary(summary);
                news.setContent(content);
                news.setCreatedate(createdate);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return news;
    }

    @Override
    public List<News> selectPage(int index, int crencount) {
        List<News> newsList = new ArrayList<News>();
        ResultSet resultSet=null;
        String sql="SELECT * FROM `news_detail`  LIMIT ?,?";
        Object[] obj={index,crencount};
        resultSet=this.selectPageS(sql,obj);
        try {
            while (resultSet.next()){
                News  news = new News();
                int id=  resultSet.getInt("id");
                int categoryId=  resultSet.getInt("categoryId");
                String title=  resultSet.getString("title");
                String summary=  resultSet.getString("summary");
                String content=  resultSet.getString("content");
                Date createdate= resultSet.getDate("createdate");
                String picPath= resultSet.getString("picPath");
                //利用实体类封装数据
                news.setId(id);
                news.setCategoryId(categoryId);
                news.setTitle(title);
                news.setSummary(summary);
                news.setContent(content);
                news.setCreatedate(createdate);
                news.setPicPath(picPath);
                //将javabean对象存储到集合中---临时存储
                newsList.add(news);
            }
        }catch (Exception e){
            e.printStackTrace();
        }

        return newsList;
    }

    @Override
    public User getUser(String name, String password) {
            String sql="SELECT * FROM `news_user` WHERE `userName`=? AND `password`=?";
            Object[] obj={name,password};
            ResultSet resultSet= this.selectObj(sql,obj);
            User user =null;
           try {
               while (resultSet.next()){
                     user = new User();
                    user.setName(resultSet.getString("userName"));
                    user.setPass(resultSet.getString("password"));
               }
           }catch (Exception e){
               e.printStackTrace();
           }
        return user;
    }


    @Override
    public int insertNews(NewsDetail newsDetail) {
        String sql="INSERT INTO `news_detail`(`title`,`picPath`) VALUES(?,?)";
        Object[] obj={newsDetail};
        return this.InsertObjBean(sql,obj);
    }

    @Override
        public int deleteById(int id) {
             int cont =0;
           try {
               String sql="DELETE FROM `news_detail` WHERE id=?";
               Object[] obj={id};
               cont =this.updateObj(sql,obj);

           }catch (Exception e){
                e.printStackTrace();
           }
              return cont;
        }

    @Override
    public int deleteByName(String name) {
        return 0;
    }

    @Override
    public int updateById(int id) {
        return 0;
    }

    @Override
    public int updateByByName(String name) {
        return 0;
    }

    @Override
    public int updateNews(News news) {
        String sql="UPDATE `news_detail` SET `title`=? , `summary`=? ," +
                "`content`=? WHERE id=?";
        Object[] obj ={news};
        int count = this.updateObjBean(sql,obj);
        return count;
    }

}
